/*
	Copyright 2008 Jay Elliott

	This file is part of libemath.

	libemath is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	libemath is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with libemath.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __MACROS_H_
#define __MACROS_H_


#include <cmath>
//macros.h is probably a poor name for this header because there are also inline functions

#if 0

#define EPSILON (1.0f / 32.0f)

#define PI 3.14159265f
#define DEG2RAD(theta) (theta * (PI / 180.0f))
#define RAD2DEG(theta) (theta * (180.0f / PI))


#define INDEX_2D(NumCols, row, col) (col * NumCols + row)//this is broken
#endif

namespace emath
{
	//constant used to compensate for floating point precision.  see the equals(float, float) function below for an example.
	const float EPSILON = 1.0f / 32.0f;

	//obviously, it's pi.
	const float PI = 3.14159265f;

	//convertes degrees to radians
	inline float Deg2Rad(float theta)
	{
		return theta * (PI / 180.0f);
	}

	//converts radians to degrees
	inline float Rad2Deg(float theta)
	{
		return theta * (180.0f / PI);
	}

	/*
	allows you to index into a 1-dimensional array as if it were 2-dimensional.
	Technically speaking, this doesn't really belong in a 3d math library, but
	it's needed by cMatrix, which is why it's here.
	*/
	inline unsigned index2d(unsigned NumCols, unsigned row, unsigned col)
	{
		return col * NumCols + row;
	}

	//returns the larger of the two numbers
	template <typename T>
	inline T max(T x, T y)
	{
		return x > y ? x : y;
	}

	//returns the smaller of the two numbers
	template <typename T>
	inline T min(T x, T y)
	{
		return x < y ? x : y;
	}

	//This function tests to see if two floats are equal, using EPSILON to compensate for floating point precision
	inline bool equals(float left, float right)
	{
		return fabs(left - right) <= EPSILON;
	}
}
#endif
